from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.ecc.v20181213 import ecc_client, models

def query_correction_task(secret_id, secret_key, task_id):
    """查询批改任务结果"""
    try:
        # 初始化认证
        cred = credential.Credential(secret_id, secret_key)
        
        # 配置HTTP协议
        http_profile = HttpProfile()
        http_profile.endpoint = "ecc.tencentcloudapi.com"
        
        # 初始化客户端
        client_profile = ClientProfile()
        client_profile.httpProfile = http_profile
        client = ecc_client.EccClient(cred, "ap-guangzhou", profile=client_profile)

        # 修正请求类名
        req = models.DescribeTaskRequest()
        req.TaskId = task_id
        
        # 修正调用方法
        response = client.DescribeTask(req)
        
        # 处理响应状态
        if response.Data.TaskStatus == 2:  # 2表示任务成功
            return response.Data
        else:
            raise RuntimeError(f"任务处理中，当前状态：{response.Data.TaskStatus}")
            
    except Exception as e:
        raise RuntimeError(f"查询失败: {str(e)}")

# 使用示例
if __name__ == "__main__":
    import configparser
    
    try:
        # 读取配置文件
        config = configparser.ConfigParser()
        config.read(r'd:\AiPro\english\config.ini')
        
        SECRET_ID = config.get('Tencent', 'secret_id')
        SECRET_KEY = config.get('Tencent', 'secret_key')
        TASK_ID = "5fe3e3af-218a-4a8b-8934-c1f18c9cf6a1"  # 替换为实际任务ID
        
        result = query_correction_task(SECRET_ID, SECRET_KEY, TASK_ID)
        
        # 解析结果
        print(f"综合评分：{result.TotalScore}")
        print("错误详情：")
        for error in result.Errors:
            print(f"- 位置[{error.StartPosition}-{error.EndPosition}]")
            print(f"  原句: {error.OriginalText}")
            print(f"  建议: {error.CorrectedText}")
            
    except configparser.NoSectionError:
        print("配置错误：缺少[Tencent]段落")
    except configparser.NoOptionError as e:
        print(f"配置缺失：{str(e)}")
    except Exception as e:
        print(f"查询异常：{str(e)}")